package com.mingqi.enetconnected.primary.mytask.taskDetails;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mingqi.enetconnected.R;
import com.mingqi.enetconnected.base.BaseActivity;
import com.mingqi.enetconnected.base.BaseFragment;
import com.mingqi.enetconnected.primary.mytask.activity.AssginTaskActivity;
import com.mingqi.enetconnected.primary.mytask.activity.CancelTaskActivity;
import com.mingqi.enetconnected.primary.mytask.activity.EquityCheckActivity;
import com.mingqi.enetconnected.primary.mytask.activity.TaskDetailsActivity;
import com.mingqi.enetconnected.primary.mytask.entity.UserInfoMultipleEntity;
import com.mingqi.enetconnected.primary.mytask.adapter.UserInfoAdapter;
import com.mingqi.enetconnected.primary.mytask.event.EquityCheckEvent;
import com.mingqi.enetconnected.primary.mytask.event.HintNumberEvent;
import com.mingqi.enetconnected.primary.mytask.event.OrderStateEvent;
import com.mingqi.enetconnected.primary.mytask.event.ScrollEvent;
import com.mingqi.enetconnected.util.hint.ToastUtils;
import com.mingqi.enetconnected.util.resource.ResourceUtils;
import com.mingqi.enetconnected.view.Dialog.CommonDialog;
import com.mingqi.enetconnected.view.Dialog.QrCodeDialog;
import com.mingqi.enetconnected.view.QrCodePayDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by li on 2018/8/13.
 * WeChat 18571658038
 * author LiWei
 */

public class UserInfoFragment extends BaseFragment {

    @BindView(R.id.rv_user_info)
    RecyclerView mRvUserInfo;
    @BindView(R.id.srl_user_info)
    SwipeRefreshLayout mSrlUserInfo;
    private CommonDialog mSignDialog;
    private UserInfoAdapter adapter;
    private ArrayList<UserInfoMultipleEntity> mMultipleEntities;

    @Override
    protected View bindView(ViewGroup container) {

        View view = LayoutInflater.from(mContext).inflate(R.layout.fragment_user_info, container, false);
        return view;
    }

    @Override
    protected void initView() {

        mMultipleEntities = new ArrayList<>();

        mMultipleEntities.add(new UserInfoMultipleEntity(UserInfoMultipleEntity.USER_INFO, "用户信息"));
        mMultipleEntities.add(new UserInfoMultipleEntity(UserInfoMultipleEntity.EQUITY_DATA, "产权资料"));
        mMultipleEntities.add(new UserInfoMultipleEntity(UserInfoMultipleEntity.PAYMENT_INFO, "缴费信息"));
        mMultipleEntities.add(new UserInfoMultipleEntity(UserInfoMultipleEntity.SERVICE_CONTENT, "服务内容"));
        mMultipleEntities.add(new UserInfoMultipleEntity(UserInfoMultipleEntity.REPLENISH_STATE, "补充说明"));
        mMultipleEntities.add(new UserInfoMultipleEntity(UserInfoMultipleEntity.ORDER_SOURCE, "订单来源"));
        mMultipleEntities.add(new UserInfoMultipleEntity(UserInfoMultipleEntity.ORDER_OPERATE, "订单操作"));

        adapter = new UserInfoAdapter(mMultipleEntities);
        mRvUserInfo.setAdapter(adapter);
        mRvUserInfo.setLayoutManager(new LinearLayoutManager(mContext));

        mSrlUserInfo.setColorSchemeColors(mContext.getResources().getColor(R.color.colorPrimary));

        mSrlUserInfo.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                mRvUserInfo.postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        mSrlUserInfo.setRefreshing(false);
                    }
                },1000);
            }
        });

        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

                int id = view.getId();
                switch (id) {
                    case R.id.tv_equity_check :

                        startActivity(EquityCheckActivity.class);

                        break;
                    case R.id.tv_scan_sign :

                        showSignDialog();

                        break;
                    case R.id.tv_assist_sign :

                        Toast.makeText(mContext, "点击了协助签订", Toast.LENGTH_SHORT).show();

                        break;
                    case R.id.rtv_pay :

                        showPayDialog();

                        break;
                    case R.id.iv_service_message :

                        Toast.makeText(mContext, "调到即时通讯界面", Toast.LENGTH_SHORT).show();

                        break;
                    case R.id.iv_service_phone :

                        jump2Call("10086");

                        break;
                    case R.id.rtv_cancel :

                        cancelTask();

                        break;
                    case R.id.rtv_change_dispatch:

                        assignTask();

                        break;
                    case R.id.rtv_go_sign :

                        showGoSign();

                        break;
                }
            }
        });
    }

    private void jump2Call(String phone) {

        Intent intent = new Intent(Intent.ACTION_DIAL);
        Uri data = Uri.parse("tel:" + phone);
        intent.setData(data);
        mContext.startActivity(intent);
    }

    private void CancelOrder() {

        CommonDialog dialog = new CommonDialog(mContext, "确定取消订单");
        dialog.show();

        dialog.setOnConfirmListener(new CommonDialog.OnConfirmListener() {
            @Override
            public void setOnConfirmClick(CommonDialog instance) {

                Toast.makeText(mContext, "取消订单", Toast.LENGTH_SHORT).show();
                instance.dismiss();

                EventBus.getDefault().post(new OrderStateEvent(true));
                BaseActivity baseActivity = (BaseActivity) mContext;
                baseActivity.finish();
            }
        });
    }

    private void showPayDialog() {

        QrCodePayDialog payDialog = new QrCodePayDialog(mContext,"212312");
        payDialog.show();
    }

    private void showSignDialog() {

        QrCodeDialog dialog = new QrCodeDialog(mContext, "1213");
        dialog.show();
    }

    private void assignTask() {
        startActivity(AssginTaskActivity.class);
    }

    private void cancelTask() {
        startActivity(CancelTaskActivity.class);
    }

    private void showGoSign() {

        if(mSignDialog == null) {

            mSignDialog = new CommonDialog(mContext, ResourceUtils.getString(R.string.confirm_go_sign));
        }
        mSignDialog.show();

        mSignDialog.setOnConfirmListener(new CommonDialog.OnConfirmListener() {
            @Override
            public void setOnConfirmClick(CommonDialog instance) {

                instance.dismiss();
                Toast.makeText(mContext, "已签到", Toast.LENGTH_SHORT).show();
                EventBus.getDefault().post(new ScrollEvent(true));
                EventBus.getDefault().post(new HintNumberEvent(0));
                adapter.remove(mMultipleEntities.size() - 1);
            }
        });
    }

    @Override
    protected void initClick() {

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        EventBus.getDefault().register(this);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onDestroyView() {

        EventBus.getDefault().unregister(this);
        super.onDestroyView();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void hasCheckPass(EquityCheckEvent event){

        boolean check = event.isCheck();
        if(check) {

            adapter.setData(1,new UserInfoMultipleEntity(UserInfoMultipleEntity.EQUITY_DATA, "产权资料通过"));
            adapter.notifyItemChanged(1);
        } else {

            adapter.setData(1,new UserInfoMultipleEntity(UserInfoMultipleEntity.EQUITY_DATA, "产权资料不通过"));
            adapter.notifyItemChanged(1);
        }
    }
}
